import math


def parse_digits(x, base=10):
    digits = []
    while x > 0:
        digit = x % base
        digits.append(digit)
        x -= digit
        x //= base
    digits.reverse()
    return digits


def count4(x):
    a = parse_digits(x)
    a.reverse()
    n = len(a)

    result = 0
    any4 = False
    for i in range(n-1, -1, -1):
        digit = a[i]
        if any4:
            result += digit * 10 ** i
            continue

        if digit == 0:
            continue

        a_bar = digit - 1 if digit > 4 else digit
        result += a_bar * sum(math.comb(i, j) * 9 ** (i - j) for j in range(1, i + 1))
        if digit == 4:
            result += 1
        elif digit > 4:
            result += 10 ** i
        any4 |= digit == 4
    return result


def solve(x):
    if '4' in str(x):
        return None
    return x - count4(x)


def test_count4():
    def brutal_count4(x):
        count = 0
        for i in range(x + 1):
            if '4' in str(i):
                count += 1
        return count

    for x in range(10000):
        res1 = count4(x)
        res2 = brutal_count4(x)
        if res1 != res2:
            print(x, res1, res2)
            assert False
test_count4()


def test():
    for x in range(1, 200):
        print(x, solve(x))

    for e in range(100):
        print(e, solve(10**e)/10**e, 0.9**e)
test()